import java.math.BigDecimal;

public class JDecimal implements Decimal {

  public JDecimal (String src) { _ref = new BigDecimal(src); }

  @Override
  public void release () { _ref = null; }

  @Override
  public int cmp (Decimal other) { return _ref.compareTo(((JDecimal)other)._ref); }

  @Override
  public Decimal add (Decimal other) { return new JDecimal(_ref.add(((JDecimal)other)._ref)); }

  @Override
  public Decimal sub (Decimal other) { return new JDecimal(_ref.subtract(((JDecimal)other)._ref)); }

  @Override
  public Decimal mul (Decimal other) { return new JDecimal(_ref.multiply(((JDecimal)other)._ref)); }

  @Override
  public Decimal div (Decimal other, RoundMode mode) { return new JDecimal(_ref.divide(((JDecimal)other)._ref, Math.max(_ref.scale(), ((JDecimal)other)._ref.scale()), mode.j)); }

  @Override
  public Decimal neg () { return new JDecimal(_ref.negate()); }

  @Override
  public Decimal abs () { return new JDecimal(_ref.abs()); }

  @Override
  public Decimal pow (int exp) { return new JDecimal(_ref.pow(exp)); }

  @Override
  public Meta meta () {
    Meta meta = new Meta();
    meta.sign = _ref.signum();
    meta.precision = _ref.scale();
    meta.significant = _ref.precision();
    meta.value = _ref.toPlainString();
    return meta;
  }


  private JDecimal (BigDecimal ref) { _ref = ref; }

  private BigDecimal _ref;
}
